var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var address = require("../../modules/saveAddress.js");
var time = 0;
var touchDot = 0;//触摸时的原点
var interval = "";
var App = getApp();
Page({
  data: {
    addressList: [],
    addressIndex:-1,
  },
  onLoad: function (options) {  
    const _this = this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        
        _this.getAllAddressList(latitude, longitude);
      }
     })
  },
  getAllAddressList: function(lat, lon){
    let _this = this;
    util.request(api.store.addressAllList, {
      lat,
      lon
    }).then(res=>{
      this.setData({
        addressList: res.result,
      });
      // let addressInfo = wx.getStorageSync('location');
      // if(addressInfo){
        let index = this.data.addressList.findIndex((item)=>{
            return item.isChoose
        })
        this.setData({
          addressIndex:index || 0,
        })
        
      // }
    }).catch(err=>{
      console.log(err)
    })
  },
  async choiceAddress(event) {
    let addressInfo = this.data.addressList[event.currentTarget.dataset.itemIndex];
    this.setData({
      addressIndex:event.currentTarget.dataset.itemIndex
    })

    wx.setStorageSync('memberLeaderObj', addressInfo);
    await address._saveAddress(addressInfo.id);
    wx.navigateBack();

  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  
})